package com.ruoyi.wx.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.wx.domain.WxUser;
import com.ruoyi.wx.service.IWxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.TGoodsCart;
import com.ruoyi.wx.service.ITGoodsCartService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-05-04
 */
@Controller
@RequestMapping("/wx/cart")
public class TGoodsCartController extends BaseController {
    private String prefix = "wx/cart";

    @Autowired
    private ITGoodsCartService tGoodsCartService;
    @Autowired
    private IWxUserService wxUserService;

    @RequiresPermissions("wx:cart:view")
    @GetMapping()
    public String cart() {
        return prefix + "/cart";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("wx:cart:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TGoodsCart tGoodsCart) {
        startPage();
        List<TGoodsCart> list = tGoodsCartService.list(new QueryWrapper<>(tGoodsCart));
        list.forEach(goodsCart->{
            goodsCart.setUserName(wxUserService.getOne(new QueryWrapper<>(new WxUser().setId(goodsCart.getUserId()))).getNickName());
        });
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("wx:cart:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TGoodsCart tGoodsCart) {
        List<TGoodsCart> list = tGoodsCartService.list(new QueryWrapper<>(tGoodsCart));
        ExcelUtil<TGoodsCart> util = new ExcelUtil<>(TGoodsCart.class);
        return util.exportExcel(list, "cart");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("wx:cart:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TGoodsCart tGoodsCart) {
        return toAjax(tGoodsCartService.save(tGoodsCart));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{goodsCartId}")
    public String edit(@PathVariable("goodsCartId") String goodsCartId, ModelMap mmap) {
        TGoodsCart tGoodsCart = tGoodsCartService.getById(goodsCartId);
        mmap.put("tGoodsCart", tGoodsCart);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("wx:cart:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TGoodsCart tGoodsCart) {
        return toAjax(tGoodsCartService.updateById(tGoodsCart));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("wx:cart:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tGoodsCartService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
